package com.sunmnet.j2ee.action.powerdb.designer;

import com.sunmnet.j2ee.action.BaseNewAction;
import com.sunmnet.j2ee.bean.pub.action.JsonResult;
import com.sunmnet.j2ee.bean.pub.action.PageResult;
import com.sunmnet.j2ee.core.pub.QueryFilter;
import com.sunmnet.j2ee.core.pub.annotation.MethodName;
import com.sunmnet.j2ee.entity.powerdb.designer.DbConnInfo;
import com.sunmnet.j2ee.service.BaseService;
import com.sunmnet.j2ee.service.powerdb.designer.DbConnInfoService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by 蔡岳衡 on 2016/7/28.
 */

@Controller("/powerdb/designer/dbConnInfo")
@RequestMapping("/powerdb/designer/dbConnInfo")
public class DbConnInfoAction extends BaseNewAction<DbConnInfo, Long> {

    /**
     * log4j2日志 *
     */
    private static final Logger LOG = LogManager.getLogger(DbConnInfoAction.class.getName());

    @Resource(name = "/powerdb/designer/dbConnInfoService")
    @Override
    public void setService(BaseService<DbConnInfo, Long> service) {
        super.service = service;
    }

    /***************** restful start *****************************************************************************/

	/* 数据库管理功能的新增查改 */

    /**
     * 批量查询
     *
     * @param request
     * @return JsonResult
     */
    @MethodName(name = "批量查询")
    @RequestMapping(value = "/", method = RequestMethod.PATCH)
    @ResponseBody
    public JsonResult find(HttpServletRequest request) {
        QueryFilter filter = new QueryFilter(request);
        return super.find(filter);
    }

    /**
     * 批量查询
     *
     * @param request
     * @return JsonResult
     */
    @MethodName(name = "批量查询")
    @RequestMapping(value = "/pages", method = RequestMethod.PATCH)
    @ResponseBody
    public PageResult findPages(HttpServletRequest request) {
        QueryFilter filter = new QueryFilter(request);
        return super.findPages(filter);
    }

    /**
     * 查询
     *
     * @param id
     * @return JsonResult
     */
    @MethodName(name = "对象查询")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResult get(@PathVariable("id") long id, HttpServletRequest request) {
        return super.get(id);
    }

    /**
     * 新增
     *
     * @param obj
     * @return JsonResult
     */
    @MethodName(name = "新增")
    @RequestMapping(value = "/", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult add(DbConnInfo obj) {
        if (((DbConnInfoService) service).checkRepeat(obj) == 1) {
            JsonResult jsonResult = new JsonResult(false);
            jsonResult.setObj(obj);
            jsonResult.setMsg("数据库连接【" + obj.getName() + "】已经存在，请重新命名！");
            return jsonResult;
        }
        return super.add(obj);
    }

    /**
     * 更新
     *
     * @param obj
     * @return JsonResult
     */
    @MethodName(name = "修改")
    @RequestMapping(value = "/", method = RequestMethod.PUT)
    @ResponseBody
    public JsonResult update(DbConnInfo obj) {
        JsonResult jsonResult = new JsonResult(true);
        try {
            if (((DbConnInfoService) service).checkRepeat(obj) == 1) {
                jsonResult.setSuccess(false);
                jsonResult.setObj(obj);
                jsonResult.setMsg("数据库连接【" + obj.getName() + "】已经存在，请重新命名！");
                return jsonResult;
            }
            int result = ((DbConnInfoService) service).updateDbConnInfo(obj);
            jsonResult.setObj(result);
            if (result == 1) {
                jsonResult.setMsg("修改成功！");
            } else {
                jsonResult.setSuccess(false);
                jsonResult.setMsg("修改失败！");
            }
        } catch (Exception e) {
            setErrMsg("修改失败", LOG, jsonResult, e);
        }
        return jsonResult;
    }

    /**
     * 删除
     *
     * @param id
     * @return JsonResult
     */
    @MethodName(name = "删除")
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public JsonResult delete(@PathVariable("id") long id) {
        return super.delete(id);
    }

    /**
     * 批量删除
     *
     * @param ids 一个或多个id "1,2,3"
     * @return JsonResult
     */
    @MethodName(name = "批量删除")
    @RequestMapping(value = "/batch", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult batchDelete(@RequestParam(value = "ids", required = true) String ids) {
        return super.delete(ids);
    }

/***************** restful end *****************************************************************************/

	/* 用户管理功能 */

    /**
     * 查询指定数据库关联的账户名列表
     *
     * @param dbId
     * @return 返回关联的账户名列表
     */
    @MethodName(name = "查询数据库关联账户名")
    @RequestMapping(value = "/getDbRelAccount/{id}", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult getDbRelAccount(@PathVariable("id") long dbId) {

        JsonResult json = new JsonResult(true);
        try {
            json.setObj(((DbConnInfoService) service).getDbRelAccount(dbId));
            json.setMsg("查询成功！");
        } catch (Exception e) {
            setErrMsg("查询失败", LOG, json, e);
        }
        return json;
    }

    /**
     * 给选定数据库添加关联的账户列表
     *
     * @param dbId       指定的数据库id
     * @param accountIds 一个或多个账户id "1,2,3"
     * @return
     */
    @MethodName(name = "给选定数据库添加关联的账户列表")
    @RequestMapping(value = "/addDbRelAccount", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult saveDbRelAccount(@RequestParam(value = "id", required = true) Long dbId,
                                       @RequestParam(value = "ids", required = true) String accountIds) {

        JsonResult json = new JsonResult(true);
        /*
		*  给选定数据库添加关联的账户列表
		*  先删除后添加，确保没有重复添加
		*/
        try {
            ((DbConnInfoService) service).deleteDbRelateAccount(dbId, accountIds);
            json.setObj(((DbConnInfoService) service).saveDbRelateAccount(dbId, accountIds));
        } catch (Exception e) {
            setErrMsg("添加数据库与账户关系失败", LOG, json, e);
        }
        return json;
    }

    /**
     * 删除数据库已连接的账户列表
     *
     * @param dbId       指定的数据库id
     * @param accountIds 一个或多个账户id "1,2,3"
     * @return
     */
    @MethodName(name = "删除数据库已连接的账户列表")
    @RequestMapping(value = "/delDbRelAccount", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult deleteDbRelAccount(@RequestParam(value = "id", required = true) Long dbId,
                                         @RequestParam(value = "ids", required = true) String accountIds) {

        JsonResult json = new JsonResult(true);
		/*
		*  删除数据库已连接的账户列表
		*/
        try {

            json.setObj(((DbConnInfoService) service).deleteDbRelateAccount(dbId, accountIds));
        } catch (Exception e) {
            setErrMsg("删除数据库与账户关系失败", LOG, json, e);
        }
        return json;
    }

    /**
     * 查询数据库连接信息
     *
     * @param request
     * @return JsonResult
     */
    @MethodName(name = "查询数据库连接信息")
    @RequestMapping(value = "/findDBInfo", method = RequestMethod.PATCH)
    @ResponseBody
    public JsonResult findDBInfo(HttpServletRequest request) {

        JsonResult json = new JsonResult(true);
        QueryFilter filter = new QueryFilter(request);
        try {
            json.setObj(((DbConnInfoService) service).getDbConnInfo(filter));
            json.setMsg("查询数据库连接信息成功");
        } catch (Exception e) {
            setErrMsg("查询数据库连接信息失败", LOG, json, e);
        }
        return json;
    }

    /**
     * 修改连接信息密码
     *
     * @param id
     * @param password
     * @return JsonResult
     */
    @MethodName(name = "修改连接信息密码")
    @RequestMapping(value = "/updatePassword", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult updatePassword(@RequestParam(value = "id", required = true) Long id,
                                     @RequestParam(value = "password", required = true) String password) {

        JsonResult json = new JsonResult(true);
        try {
            int result = ((DbConnInfoService) service).updatePassword(id, password);
            json.setObj(result);
            if (result == 1) {
                json.setMsg("修改连接信息密码成功");
            } else {
                json.setSuccess(false);
                json.setMsg("修改连接信息密码失败");
            }
        } catch (Exception e) {
            setErrMsg("修改连接信息密码失败", LOG, json, e);
        }
        return json;
    }

}
